package com.driving.single.frontend.portal.service.impl;

import com.driving.single.common.content.service.CommonContentService;
import com.driving.single.common.domain.TbCoachComment;
import com.driving.single.common.domain.TbContent;
import com.driving.single.common.domain.TbPartnerComment;
import com.driving.single.common.domain.TbSchoolComment;
import com.driving.single.common.entity.DrivingResult;
import com.driving.single.common.entity.RecommendResult;
import com.driving.single.common.mapper.CustomPortalRecommendMapper;
import com.driving.single.common.redis.service.CommonRedisFrontendPortalService;
import com.driving.single.frontend.portal.dto.PortalAdDTO;
import com.driving.single.frontend.portal.service.PortalContentService;
import org.assertj.core.util.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 首页：内容
 *
 * @author xsf
 * @version v1.0.0
 * @date 2019/12/15 0015
 * @see com.driving.single.frontend.portal.service.impl
 **/
@Service
public class PortalContentServiceImpl implements PortalContentService {
    /**
     * 插入Redis评论最小长度
     */
    private static final int INSERT_REDIS_COMMENT_MINLEN = 6;

    /**
     * 大广告分类ID
     */
    @Value("${PORTAL_BAD_CID}")
    private Long portalBadCid;
    /**
     * 小广告分类ID
     */
    @Value("${PORTAL_SAD_CID}")
    private Long portalSadCid;
    /**
     * 中部广告分类ID
     */
    @Value("${PORTAL_MAD_CID}")
    private Long portalMadCid;
    /**
     * 驾校边栏广告分类ID
     */
    @Value("${PORTAL_SCH_SIDE_CID}")
    private Long portalSchSideCid;
    /**
     * 驾校推荐广告分类ID
     */
    @Value("${PORTAL_SCH_RECOMM_CID}")
    private Long portalSchRecommCid;
    /**
     * 教练推荐广告分类ID
     */
    @Value("${PORTAL_COACH_RECOMM_CID}")
    private Long portalCoachRecommCid;
    /**
     * 陪练推荐广告分类ID
     */
    @Value("${PORTAL_PARTNER_RECOMM_CID}")
    private Long portalPartnerRecommCid;
    /**
     * 尾部广告分类ID
     */
    @Value("${PORTAL_FOOT_AD_CID}")
    private Long portalFootAdCid;

    @Resource
    private CommonContentService commonContentService;
    @Resource
    private CommonRedisFrontendPortalService commonRedisFrontendPortalService;

    @Resource
    private CustomPortalRecommendMapper customPortalRecommendMapper;

    /**
     * 查询首页大广告位
     */
    @Override
    public DrivingResult selectFrontendContentBad() {
        return this.selectFrontendContentByCategoryId(this.portalBadCid);
    }

    /**
     * 首页小广告位接口
     */
    @Override
    public DrivingResult selectFrontendContentSmallAd() {
        return this.selectFrontendContentByCategoryId(this.portalSadCid);
    }

    /**
     * 首页中部广告位接口
     */
    @Override
    public DrivingResult selectFrontendContentMiddleAd() {
        return this.selectFrontendContentByCategoryId(this.portalMadCid);
    }

    /**
     * 首页驾校边栏广告位接口
     */
    @Override
    public DrivingResult selectPortalSchSideAd() {
        return this.selectFrontendContentByCategoryId(this.portalSchSideCid);
    }

    /**
     * 首页驾校推荐广告位接口
     */
    @Override
    public DrivingResult selectPortalSchRecommAd() {
        // 1.查询Redis缓存
        try {
            List<RecommendResult> redisRecommends =
                    this.commonRedisFrontendPortalService.selectRedisRecommendContentWithCid(this.portalSchRecommCid);
            if (redisRecommends != null && redisRecommends.size() > 0) {
                return DrivingResult.ok(redisRecommends);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 2.查询数据库
        List<RecommendResult> list =
                this.customPortalRecommendMapper.selectRecommendSchoolList(this.portalSchRecommCid);
        if (list == null || list.size() == 0) {
            return DrivingResult.error("查无数据");
        }
        for (RecommendResult recommendResult : list) {
            System.out.println(recommendResult);
            // 并未选择最优评论，自动随机选取最高分评论
            if (recommendResult.getCommentId() == null) {
                TbSchoolComment tbSchoolComment =
                        this.customPortalRecommendMapper.selectRandomBestRecommendSchoolComment(recommendResult.getTargetId());
                if (tbSchoolComment != null) {
                    BeanUtils.copyProperties(tbSchoolComment, recommendResult);
                }
            }
        }
        // 3.插入Redis内存
        if (list.size() == INSERT_REDIS_COMMENT_MINLEN) {
            try {
                this.commonRedisFrontendPortalService.insertRedisRecommendContentWithCid(list, this.portalSchRecommCid);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return DrivingResult.ok(list);
    }

    /**
     * 首页陪练推荐广告位接口
     */
    @Override
    public DrivingResult selectPortalSparRecommAd() {
        // 1.查询Redis缓存
        try {
            List<RecommendResult> redisRecommends =
                    this.commonRedisFrontendPortalService.selectRedisRecommendContentWithCid(this.portalPartnerRecommCid);
            if (redisRecommends != null && redisRecommends.size() > 0) {
                return DrivingResult.ok(redisRecommends);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 2.查询数据库
        List<RecommendResult> list =
                this.customPortalRecommendMapper.selectRecommendPartnerList(this.portalPartnerRecommCid);
        if (list == null || list.size() == 0) {
            return DrivingResult.error("查无数据");
        }
        for (RecommendResult recommendResult : list) {
            System.out.println(recommendResult);
            // 并未选择最优评论，自动随机选取最高分评论
            if (recommendResult.getCommentId() == null) {
                TbPartnerComment tbPartnerComment =
                        this.customPortalRecommendMapper.selectRandomBestRecommendPartnerComment(recommendResult.getTargetId());
                if (tbPartnerComment != null) {
                    BeanUtils.copyProperties(tbPartnerComment, recommendResult);
                }
            }
        }
        // 3.插入Redis内存
        if (list.size() == 6) {
            try {
                this.commonRedisFrontendPortalService.insertRedisRecommendContentWithCid(list,
                        this.portalPartnerRecommCid);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return DrivingResult.ok(list);
    }

    /**
     * 首页尾部广告位接口
     */
    @Override
    public DrivingResult selectPortalFootAd() {
        return this.selectFrontendContentByCategoryId(this.portalFootAdCid);
    }

    /**
     * 首页教练推荐广告位接口
     */
    @Override
    public DrivingResult selectPortalCoachRecommAd() {
        // 1.查询Redis缓存
        try {
            List<RecommendResult> redisRecommends =
                    this.commonRedisFrontendPortalService.selectRedisRecommendContentWithCid(this.portalCoachRecommCid);
            if (redisRecommends != null && redisRecommends.size() > 0) {
                return DrivingResult.ok(redisRecommends);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 2.查询数据库
        List<RecommendResult> list =
                this.customPortalRecommendMapper.selectRecommendCoachList(this.portalCoachRecommCid);
        if (list == null || list.size() == 0) {
            return DrivingResult.error("查无数据");
        }
        for (RecommendResult recommendResult : list) {
            System.out.println(recommendResult);
            // 并未选择最优评论，自动随机选取最高分评论
            if (recommendResult.getCommentId() == null) {
                TbCoachComment tbCoachComment =
                        this.customPortalRecommendMapper.selectRandomBestRecommendCoachComment(recommendResult.getTargetId());
                if (tbCoachComment != null) {
                    BeanUtils.copyProperties(tbCoachComment, recommendResult);
                }
            }
        }

        // 3.插入Redis内存
        if (list.size() == 4) {
            try {
                this.commonRedisFrontendPortalService.insertRedisRecommendContentWithCid(list,
                        this.portalCoachRecommCid);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return DrivingResult.ok(list);
    }

    /**
     * 封装的一个查询首页内容方法
     *
     * @param categoryId 分类ID
     */
    private DrivingResult selectFrontendContentByCategoryId(Long categoryId) {
        //1.查询Redis缓存
        try {
            List<PortalAdDTO> redisContent =
                    this.commonRedisFrontendPortalService.selectRedisFrontendContentWithCid(categoryId);
            if (redisContent != null && redisContent.size() > 0) {
                return DrivingResult.ok(redisContent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 2.查询数据库
        List<TbContent> dbContent = this.commonContentService.selectTbContentAllByCategoryId(categoryId);
        if (dbContent == null || dbContent.size() == 0) {
            return DrivingResult.error("查无数据");
        }
        // 数据封装
        List<PortalAdDTO> list = Lists.newArrayList();
        dbContent.forEach(tbContent -> {
            PortalAdDTO portalAdDTO = new PortalAdDTO();
            BeanUtils.copyProperties(tbContent, portalAdDTO);
            list.add(portalAdDTO);
        });

        // 3.存入Redis缓存
        try {
            this.commonRedisFrontendPortalService.insertRedisFrontendContentWithCid(list, categoryId);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return DrivingResult.ok(list);
    }
}
